package heip.generator.generer;

import heip.generator.entity.EntityTable;
import heip.generator.entity.TableClazz;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 实体生成
 * @author HSWHM
 *
 *
 */
public class EntityGenerator extends AbstractGenerator{

	private String moduleName = null;//模块
	
	
	public void execute() throws Exception{}
	
	
	/**                                                    **/
	/**-------------------------------------END 一下不用管------------ **/
	 /**                                                    */
	private String baseTem = "TEMPLATE_1.0/model/base";
	private String treeTem = "TEMPLATE_1.0/model/tree";
	private String auditTem = "TEMPLATE_1.0/model/audit";
	
	public static void main(String[] args) throws Exception{
		EntityGenerator eg = new EntityGenerator();
		eg.execute();
		open();
	}
	public EntityGenerator(){}
	
	public EntityGenerator(String moduleName){
		this.moduleName = moduleName;
	}
	
	
	/**
	 * 生成PO
	 * @param sequence  是否添加Hibernatne Id递增
	 * @param tables
	 * @throws Exception
	 */
	public void generatorModel(EntityTable table,TableClazz tableClazz,String modelType) throws Exception{
		
			//1.选择相应的模板
			//"基本","树","审核"
			String temName= null;
			if("树".equals(modelType)){temName = treeTem;}
			else if("审核".equals(modelType)){temName = auditTem;}
			else{temName= baseTem;}
			String templatePath = getRealTemPath(temName);
			
			//2 赋值fremaker对象
			//String mainTableName= table.getTableName();
			Map<String,Object> map = new HashMap<String,Object>();
			
			//TableClazz model = new TableClazz(mainTableName);
			map.put("model", tableClazz);
			
			//全局变量
			gg.put("moduleName", this.moduleName);
			gg.put("idGenerType",table.getIdGenerType());
			gg.put("now", new Date());
			
			if("sequence".equals(table.getIdGenerType())){
				String sequenceName = null;
				if(table.getSequenceName()==null||table.getSequenceName().equals("")){
					sequenceName = "SEQ_"+table.getTableName().toUpperCase();
				}else{
					sequenceName = table.getSequenceName();
				}
				gg.put("sequenceName", sequenceName);
			}
			
			
			//3生成
			generator.generateByMap(map, templatePath);
			
			if(tableClazz.getSubClazzes()!=null&&tableClazz.getSubClazzes().size()>0){
				for(TableClazz subClazz:tableClazz.getSubClazzes()){
					map.put("model", subClazz);
					generator.generateByMap(map, templatePath);
				}
			}
	}
	
	//

}